package cn.xuchunh.campusbbs.net;

import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.utils.StringUtil;

/**
 * Created by XuChunH on 2016/3/7.
 */
public class XJsonRequest<T> extends Request<T> {

    public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36";

    private XJsonListener<T> listener;
    private Map<String, String> params;
    private Map<String, String> headers;

    public XJsonRequest(int method, String url, XJsonListener<T> listener, Response.ErrorListener error) {
        this(method, url, null, listener, error);
    }

    public XJsonRequest(int method, String url, Map<String, String> headers, XJsonListener<T> listener, Response.ErrorListener error) {
        this(method, url, headers, null, listener, error);
    }

    public XJsonRequest(int method, String url, Map<String, String> headers, Map<String, String> params, XJsonListener<T> listener, Response.ErrorListener error) {
        super(method, url, error);
        this.listener = listener;
        this.params = params;
        if (headers == null) {
            headers = new HashMap<>();
            headers.put("User-Agent", USER_AGENT);
        }
        this.headers = headers;
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        String parsed = "";
        String rawCookies = response.headers.get("Set-Cookie");
        if (!TextUtils.isEmpty(rawCookies)) {
            HttpRequestManager.getInstance(AppContext.getInstance()).setCookies(rawCookies);
        }
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        } finally {
            parsed = StringUtil.decodeUnicode(parsed);
        }
        if (listener != null) {
            try{
                return Response.success(listener.parseJson(parsed), HttpHeaderParser.parseCacheHeaders(response));
            }catch (Exception e){
                return Response.error(new VolleyError(e.getMessage()));
            }
        }else {
            return Response.success(null, HttpHeaderParser.parseCacheHeaders(response));
        }
    }

    @Override
    protected void deliverResponse(T response) {
        if (listener != null) {
            listener.onResponse(response);
        }
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return params;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return headers;
    }
}
